package core.process;
import core.utils.EPara;
import core.process.pack.QualPack;
import core.rb.PB;
import core.rb.r.PR;
import gui.processor.Processor;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Qual{
    private PB pb;
    private Processor p;
    public Qual(PB pab, Processor gp){this.pb = pab;this.p = gp;}
    public ArrayList <Integer> [] getQScores() throws Exception{
        int size = (int)((pb.getSize() / EPara.THREADS));
        int i = 0;
        ArrayList <PB> packs = new ArrayList();
        ArrayList <PR> pack ;
        for(int x = 0; x<EPara.THREADS; x++){
            pack = new ArrayList();
            for(int y = 0; y<size; y++){pack.add(pb.getRead(i++));}
            packs.add(new PB(pb.getRef(), pack));
        }
        PB fPack = packs.get(0);
        for(int x=i; x<pb.getSize(); x++){fPack.add(pb.getRead(i++));}
        QualPack [] qp = new QualPack[packs.size()];
        CountDownLatch cdl = new CountDownLatch(packs.size());
        for (int x = 0; x < packs.size(); x++) {
            if(x==0){qp[x] = new QualPack(p, cdl, packs.get(x));}
            else{qp[x] = new QualPack(null, cdl, packs.get(x));}
        }
        cdl.await();
        ArrayList <Integer> [] combined = new ArrayList[pb.getRef().getDNA().length()];
        for(int s=0; s<pb.getRef().getDNA().length(); s++){combined[s] = new ArrayList();}
        for (int x = 0; x < qp.length; x++) {
            ArrayList <Integer> [] packVals = qp[x].getPOut();
            for(int s=0; s<packVals.length; s++){combined[s].addAll(packVals[s]);}
        }
        return combined;
    }
}